package com.hulu.yec.teach.examrecords.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.hulu.yec.common.Constant;
import com.hulu.yec.common.PageModel;
import com.hulu.yec.common.util.LogUtil;
import com.hulu.yec.sys.user.service.SysUserDao;
import com.hulu.yec.teach.examrecords.entity.ExamRecordsEntity;
import com.hulu.yec.teach.examrecords.entity.RecordCondition;

public class ExamRecordDao {

	/**
	 * 分页条件查询成绩记录
	 * 
	 * @param recordCondition
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PageModel<ExamRecordsEntity> examrelist(RecordCondition recordCondition ,int pageNo, int pageSize) {
		//参数设置
		StringBuilder sb = new StringBuilder();
		List<Object> params = new ArrayList<>();
		
		boolean isExistClassName = StringUtils.isNoneBlank(recordCondition.getClassName());
		boolean isExistUserName = StringUtils.isNoneBlank(recordCondition.getUserName());
		boolean isExistPaperName = StringUtils.isNoneBlank(recordCondition.getPaperName());
		
		sb.append("SELECT u.name AS 'sysUser.name',");
		sb.append("qp.name AS 'qesExamPaper.name',");
		sb.append("r.start_time,r.submit_time,");
		sb.append("qp.total_point AS 'qesExamPaper.totalPoint',");
		sb.append("r.get_point,r.is_pass FROM exam_records r,qes_exam_paper qp,sys_user u,exam_plan p ");
		//如果存在班级名称就应该将班级表添加进去
		if(isExistClassName){
			sb.append(" ,sys_class c ");
		}
		sb.append(" WHERE u.id=r.user_id AND r.exam_plan_id=p.id AND p.exam_paper_id=qp.id");
		//如果存在班级名称就应该将班级表中班级名给对应出来
		if(isExistClassName){
			sb.append(" AND c.id=u.class_id AND c.name=? ");
			params.add(recordCondition.getClassName());
		}
		if(isExistPaperName){
			sb.append(" AND qp.name LIKE '%"+recordCondition.getPaperName()+"%' ");
		}
		if(isExistUserName){
			sb.append(" AND u.name LIKE '%"+recordCondition.getUserName()+"%' ");
		}
		if(recordCondition.getState() != 0){
			sb.append(" AND r.is_pass=? ");
			params.add(recordCondition.getState());
		}
		sb.append(" AND r.get_point>=? ");
		params.add(recordCondition.getStartPoint());
		if(recordCondition.getEndPoint() != 0){
			sb.append(" AND r.get_point<=? ");
			params.add(recordCondition.getEndPoint());
		}
		sb.append(" ORDER BY u.id DESC");
		sb.append(" LIMIT ?,? ");
		LogUtil.i(Constant.LOGGER, sb.toString());
		
		
		params.add((pageNo - 1) * pageSize);
		params.add(pageSize);
		
		//执行查询
		return new SysUserDao().getPageModelEntity(ExamRecordsEntity.class, pageNo, pageSize, sb.toString(), params);
	}
	
}
